package com.li.service;

import com.li.dao.WorkMapper;
import com.li.pojo.Work;
import com.li.pojo.WorkDto;
import com.li.util.MyStringUtil;
import com.li.util.MyTimeUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkServiceImpl  implements WorkService{
    @Autowired
    private WorkMapper workMapper;
    @Override
    public WorkDto selectOne(int id) {
        return workMapper.selectOne(id);
    }

    @Override
    public List<WorkDto> selectWork(Map<String,String> map) {
        //数据筛选 清理
        Map<String,Object> map1 = new HashMap<>();
        String id=map.get("id");
        String date=map.get("date");
        String uid = map.get("uid");
        String page=map.get("page");
        String pageSize=map.get("pageSize");
        String name=map.get("name");
        if (MyStringUtil.isNullOrEmpty(id)){
            map1.put("id",0);
        }else {
            int newId=Integer.parseInt(id);
            map1.put("id",newId);
        }
        if (!MyStringUtil.isNullOrEmpty(date)){
//                map1.put("date", java.sql.Date.valueOf(date));
            map1.put("date",date);
        }
        if (!MyStringUtil.isNullOrEmpty(name)){
            map1.put("name","%"+name+"%");
        }

        if (MyStringUtil.isNullOrEmpty(uid)){
            map1.put("uid",0);
        }else {
            int newUid=Integer.parseInt(uid);
            map1.put("uid",newUid);
        }
        int newPage = Integer.parseInt(page);
        int newPageSize = Integer.parseInt(pageSize);
        map1.put("page",(newPage-1)*newPageSize);
        map1.put("pageSize",newPageSize);
        map1.put("username",map.get("username"));

        return workMapper.selectWork(map1);
    }

    @Override
    public long count(Map<String,String> map) {
        //数据筛选 清理
        Map<String,Object> map1 = new HashMap<>();
        String id=map.get("id");
        String date=map.get("date");
        String uid = map.get("uid");
        String name=map.get("name");
        if (MyStringUtil.isNullOrEmpty(id)){
            map1.put("id",0);
        }else {
            int newId=Integer.parseInt(id);
            map1.put("id",newId);
        }
        if (!MyStringUtil.isNullOrEmpty(date)){
            map1.put("date",date);
        }else {
            map1.put("date",null);
        }
        if (!MyStringUtil.isNullOrEmpty(name)){
            map1.put("name","%"+name+"%");
        }

        if (MyStringUtil.isNullOrEmpty(uid)){
            map1.put("uid",0);
        }else {
            int newUid=Integer.parseInt(uid);
            map1.put("uid",newUid);
        }
        map1.put("username",map.get("username"));
        return workMapper.count(map1);
    }

    @Override
    public int updateWork(Work work) {
        work.setModifyDate(MyTimeUtil.getCurrentTime());
        return workMapper.updateWork(work);
    }

    @Override
    public int addWork(Work work) {
        if (MyStringUtil.isNullOrEmpty(work.getDate())){
            work.setDate(MyTimeUtil.getCurrentTime());
        }

        work.setId(0);
        return workMapper.addWork(work);
    }

    @Override
    public int deleteWork(List<Integer> ids) {
        return workMapper.deleteWork(ids);
    }
    @Override
    public int editWork(WorkDto work){
        if (MyStringUtil.isNullOrEmpty(work.getModifyDate())){
            work.setModifyDate(MyTimeUtil.getCurrentTime());
        }
        return workMapper.editWork(work);
    }
}
